package com.fymxy.question_bank_management.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fymxy.question_bank_management.pojo.ApiResult;
import com.fymxy.question_bank_management.pojo.Request2teacher;
import com.fymxy.question_bank_management.pojo.Teacher;
import com.fymxy.question_bank_management.service.Request2teacherService;
import com.fymxy.question_bank_management.service.TeacherService;
import com.fymxy.question_bank_management.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;

@RestController
public class TeacherManageController {

    @Autowired
    private Request2teacherService request2teacherService;

    @Autowired
    private TeacherService teacherService;

    @GetMapping("/manage/showRequest2teacher/{page}")
    public ApiResult<Request2teacher> findAllRequest2teacher(@PathVariable("page") Integer page){
        Page<Request2teacher> request2teacherPage = new Page<>(page,10);
        IPage<Request2teacher> all = request2teacherService.findAll(request2teacherPage);

        return ApiResultHandler.buildApiResult(200,"查询所有申请成功",all);
    }

    @PostMapping("/manage/addTeacher")
    public ApiResult addTeacher(@RequestBody Request2teacher request2teacher) throws Exception {
        Teacher teacher = new Teacher();
        teacher.setTeacherDiscipline(request2teacher.getTeacherDiscipline());
        teacher.setTeacherMail(request2teacher.getTeacherMail());
        teacher.setTeacherPwd(request2teacher.getTeacherPwd());
        teacher.setTeacherName(request2teacher.getTeacherName());
        teacher.setTeacherNickname(request2teacher.getTeacherNickname());
        int addTeacherRes = teacherService.insert(teacher);//1成功，0失败
        if(addTeacherRes == 1) {
            SendMail sendMail = new SendMail(request2teacher.getTeacherMail(),"恭喜您，注册成功，现在可以登录题库管理系统了！");
            request2teacherService.deleteByPrimaryKey(request2teacher.getRequestSignupNo());
            return ApiResultHandler.buildApiResult(200, "添加teacher成功", teacher);
        }
        else
            return ApiResultHandler.buildApiResult(400,"添加teacher失败",teacher);
    }

    @DeleteMapping("/manage/refuseRequest")
    public ApiResult deleteRequest(@RequestBody Request2teacher request2teacher) throws Exception {
        int deleteRequestRes = request2teacherService.deleteByPrimaryKey(request2teacher.getRequestSignupNo());//1成功，0失败
        if(deleteRequestRes == 1) {
            SendMail sendMail = new SendMail(request2teacher.getTeacherMail(),"对不起，您的申请被拒绝，请更改信息重试！");
            return ApiResultHandler.buildApiResult(200, "撤销成功", request2teacher);
        }
        else
            return ApiResultHandler.buildApiResult(400,"撤销失败",request2teacher);
    }
}
